﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace HtmlReader
{
    public partial class Form1 : Form
    {
        string flagStart = "<TR valign=\"TOP\"><TD align=\"right\"><font size=2>";
        int flagStartLength = 48;
        string flagEnd = "&nbsp;";
        //string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"html\";    //文件目录
        string path = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "网页|*.html";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fileName = openFileDialog.SafeFileName;
                    string name = fileName.Substring(0, fileName.LastIndexOf("."));

                    path = openFileDialog.FileName.Substring(0, openFileDialog.FileName.Length - fileName.Length);

                    WebClient MyWebClient = new WebClient();

                    MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据

                    Byte[] pageData = MyWebClient.DownloadData(openFileDialog.FileName); //从指定网站下载数据

                    string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312，则使用这句            

                    //string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8，则使用这句

                    int first = pageHtml.IndexOf(flagStart);
                    int next = first;
                    int end;
                    bool flag = first >= 0;

                    if (!flag)
                    {
                        MessageBox.Show("该文件无法解析！");
                        return;
                    }

                    List<string> list = new List<string>();
                    StringBuilder sb = new StringBuilder();

                    while (flag)
                    {
                        end = pageHtml.IndexOf(flagEnd, next);

                        string id = pageHtml.Substring(next + flagStartLength, end - next - flagStartLength);
                        if (id.Length > 8)
                        {
                            id = id.Substring(id.Length - 8);
                        }
                        if (!list.Contains(id))
                        {
                            list.Add(id);
                            sb.Append(id);
                            sb.Append(",");
                        }

                        next = pageHtml.IndexOf(flagStart, next + 1);

                        flag = next >= 0;
                    }

                    //查找blank
                    int posBlank = pageHtml.IndexOf("BLANK");
                    int posX1 = pageHtml.IndexOf(" x ", posBlank);
                    int posX2 = pageHtml.IndexOf(" x ", posX1 + 3);
                    int posBlankEnd = pageHtml.IndexOf("<!--", posX2);
                    string blank = pageHtml.Substring(posX2 + 4, posBlankEnd - posX2 - 4);
                    blank = blank.Replace(Environment.NewLine, "").Trim();

                    sb.Append(blank);

                    if (!Directory.Exists(path))//若文件夹不存在则新建文件夹   
                    {
                        Directory.CreateDirectory(path); //新建文件夹   
                    }

                    QRCodeHandler qr = new QRCodeHandler();
                    string qrString = sb.ToString();                         //二维码字符串
                    string filePath = path + name + ".jpg";                                        //二维码文件名
                    qr.CreateQRCode(qrString, "Byte", 2, 0, "H", filePath, false, string.Empty);   //生成

                    //日期
                    int pos0 = pageHtml.IndexOf("<BR>", pageHtml.IndexOf("<BR>") + 4);
                    string date = pageHtml.Substring(pos0 + 4, 10);
                    //插入二维码
                    int posPic = pageHtml.IndexOf("</TABLE>", pageHtml.IndexOf("</TABLE>") + 8) + 8;
                    //pageHtml = pageHtml.Insert(posPic, "<div style='width:600px'><p style='text-align:right'><img src=\"" + name + ".jpg\"/></p></div>");
                    pageHtml = pageHtml.Insert(posPic, "</div><div style='float:right'><img src=\"" + name + ".jpg\"/><br><br>" + date + "</div></div>");
                    //                    //插入打印按钮
                    //                    int posButton = pageHtml.IndexOf("<BODY") + 37;
                    //                    string strButton = @"<div style='width:600px'><p style='text-align:right'><input type='button' onclick='printWebPage(this);' value='Print' /></p></div>";
                    //                    pageHtml = pageHtml.Insert(posButton, strButton);

                    //                    //插入打印方法
                    //                    int posPrint = pageHtml.IndexOf("</HEAD>");
                    //                    string strPrint = @"<script>
                    //                        function printWebPage(obj)
                    //                        {  
                    //                            obj.style.display='none';
                    //                            window.print(); 
                    //                            obj.style.display='';
                    //                        } 
                    //                        </script>";
                    //                    pageHtml = pageHtml.Insert(posPrint, strPrint);

                    //改变文件内容
                    int pos1 = pageHtml.IndexOf("<TABLE");
                    int pos14 = pos1;
                    int pos2 = pageHtml.IndexOf("</TABLE>");
                    pageHtml = DelMidStr(pageHtml, pos1, pos2 + 8);

                    pos1 = pageHtml.IndexOf("<TABLE");
                    pageHtml = pageHtml.Insert(pos1, @"<div style='width:750px'><div style='float:left'>");
                    pos2 = pageHtml.IndexOf("<!--HTML-Block: Fertigungsanweisungen-->");
                    pageHtml = pageHtml.Insert(pos2, @"<div style='margin-top:250px;width:750px'>");
                    int pos3 = pageHtml.IndexOf("<!--ENDE Tafellayout-->");
                    pageHtml = pageHtml.Insert(pos3, @"</div>");
                    int pos4 = pageHtml.IndexOf("STORAGE ITEM DESIGNATION:");
                    int pos5 = pageHtml.IndexOf("<TR valign", pos4 - 50);
                    int pos6 = pageHtml.IndexOf("<TR valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    pos1 = pageHtml.IndexOf("<!--Pratzendaten-->");
                    pos2 = pageHtml.IndexOf("<!--", pos1 + 3);
                    string s1 = pageHtml.Substring(pos1, pos2 - pos1);
                    s1.Replace("CLAMP POSITIONS", "夹爪位置");

                    pos1 = pageHtml.IndexOf("<!--Einlegeposition-->");
                    pos2 = pageHtml.IndexOf("<!--", pos1 + 3);
                    s1 += pageHtml.Substring(pos1, pos2 - pos1);
                    s1.Replace("STARTING POINT", "起始点");

                    pos1 = pageHtml.IndexOf("<!--Bemerkungen (z.B. Zusatzauflagetische)-->");
                    pageHtml = pageHtml.Insert(pos1, s1);

                    pos1 = pageHtml.IndexOf("<!--START Fertigungsanweisungen-->");
                    pos2 = pageHtml.IndexOf("<!--ENDE Fertigungsanweisungen-->");
                    pageHtml = DelMidStr(pageHtml, pos1, pos2);

                    //替换
                    pageHtml = pageHtml.Replace("ISO-8859-1", "utf-8");
                    pageHtml = pageHtml.Replace("MACHINE:", "机器:");
                    pageHtml = pageHtml.Replace("CONTROL:", "类型:");
                    pageHtml = pageHtml.Replace("COMPANY:", "设备名称:");
                    pageHtml = pageHtml.Replace("JOB NAME:", "文件名称:");
                    pageHtml = pageHtml.Replace("NC-PROGRAM NAME:", "路径:");
                    pageHtml = pageHtml.Replace("PROGRAM NAME:", "程序名称:");
                    pageHtml = pageHtml.Replace("MATERIAL ID:", "材质:");
                    pageHtml = pageHtml.Replace("BLANK:", "材料规格:");
                    pageHtml = pageHtml.Replace("WEIGHT:", "重量:");
                    pageHtml = pageHtml.Replace("MACHINING TIME:", "加工时间:");
                    pageHtml = pageHtml.Replace("STORAGE REQUIREMENT:", "面积:");
                    pageHtml = pageHtml.Replace("NUMBER OF PROGRAMME RUNS:", "加工张数:");
                    pageHtml = pageHtml.Replace("width=250", "width=150");

                    pageHtml = pageHtml.Replace("align=\"left\"><font size=2><b>REGRINDING&nbsp;<br>LENGTH", " width=150 align=\"left\"><font size=2><b>模具高度");
                    pageHtml = pageHtml.Replace("IDENT NO&nbsp;", "编码");
                    pageHtml = pageHtml.Replace("TYPE&nbsp;", "类型");
                    pageHtml = pageHtml.Replace("DIMENSION 1&nbsp;", "尺寸1");
                    pageHtml = pageHtml.Replace("DIMENSION 2&nbsp;", "尺寸2");
                    pageHtml = pageHtml.Replace("DIMENSION 3&nbsp;", "尺寸3");
                    pageHtml = pageHtml.Replace("ANGLE&nbsp;", "角度");
                    pageHtml = pageHtml.Replace("REMARK&nbsp;", "刀具信息");

                    //表格宽度
                    pageHtml = pageHtml.Replace("<TABLE width=600", "<TABLE width=750");

                    int pos10 = pageHtml.IndexOf("<TABLE width=750");
                    pageHtml = DelMidStr(pageHtml, pos10, pos10 + 16);
                    pageHtml = pageHtml.Insert(pos10, "<TABLE width=600");

                    //计算秒
                    int pos11 = pageHtml.IndexOf("<!--Maschinenzeit in [h:min:s] -->");
                    int pos12 = pageHtml.IndexOf("&nbsp;", pos11);
                    string time = pageHtml.Substring(pos11 + 42, pos12 - pos11 - 42);

                    string[] arr = time.Split(new string[] { " : " }, StringSplitOptions.None);
                    int hour = Convert.ToInt32(arr[0]);
                    int min = Convert.ToInt32(arr[1]);
                    int sec = Convert.ToInt32(arr[2]);

                    int secornd = hour * 60 * 60 + min * 60 + sec;

                    int pos13 = pageHtml.IndexOf("</b>", pos12 + 1);
                    pageHtml = pageHtml.Insert(pos13, secornd.ToString() + "秒");

                    //加签名
                    //int pos14 = pageHtml.IndexOf("<!--HTML-Block: Stanz-Werkzeugliste-->");
                    pageHtml = pageHtml.Insert(pos14, "完成日期：______________   系数：_______   操作员：___________   完成张数：_______   总数：__________<BR><BR><BR>完成日期：______________   系数：_______   操作员：___________   完成张数：_______   总数：__________<BR><BR>");

                    string extension = System.IO.Path.GetExtension(openFileDialog.FileName);
                    string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);

                    using (StreamWriter sw = new StreamWriter(path + fileNameWithoutExtension + "_barcode" + extension))//将获取的内容写入文本
                    {
                        sw.Write(pageHtml);
                    }

                    //打开文件
                    System.Diagnostics.Process.Start(path + fileNameWithoutExtension + "_barcode" + extension);
                }

                catch (WebException webEx)
                {
                    Console.WriteLine(webEx.Message.ToString());
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Color c = Color.FromArgb(41, 125, 194);
            //e.Graphics.DrawString("文件存储路径：", new Font("微软雅黑", 10), new SolidBrush(Color.Black), 10, 270);
            //e.Graphics.DrawString(path, new Font("微软雅黑", 10), new SolidBrush(Color.Black), 10, 285);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(path))
            {
                MessageBox.Show("请先选择文件!");
            }
            else
            {
                System.Diagnostics.Process.Start("Explorer.exe", path);
            }
        }

        private string DelMidStr(string s, int start, int end)
        {
            int len = s.Length;
            char[] s2 = new char[len];
            int i2 = 0;
            for (int i = 0; i < len; i++)
            {
                char c = s[i];
                if (i < start || i >= end)
                    s2[i2++] = c;
            }
            return new String(s2, 0, i2);
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "网页|*.html";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fileName = openFileDialog.SafeFileName;
                    string name = fileName.Substring(0, fileName.LastIndexOf("."));

                    path = openFileDialog.FileName.Substring(0, openFileDialog.FileName.Length - fileName.Length);

                    WebClient MyWebClient = new WebClient();

                    MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据

                    Byte[] pageData = MyWebClient.DownloadData(openFileDialog.FileName); //从指定网站下载数据

                    string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312，则使用这句            

                    int first = pageHtml.IndexOf("<tr valign=\"top\"><td align=\"right\"><font size=\"2\">");
                    int next = first;
                    int end;
                    bool flag = first >= 0;

                    if (!flag)
                    {
                        MessageBox.Show("该文件无法解析！");
                        return;
                    }

                    List<string> list = new List<string>();
                    StringBuilder sb = new StringBuilder();

                    while (flag)
                    {
                        end = pageHtml.IndexOf(flagEnd, next);

                        string id = pageHtml.Substring(next + 50, end - next - 50);
                        if (id.Length > 8)
                        {
                            id = id.Substring(id.Length - 8);
                        }
                        if (!list.Contains(id))
                        {
                            if (!id.Equals("99999999"))
                            {
                                list.Add(id);
                                sb.Append(id);
                                sb.Append(",");
                            }
                        }

                        next = pageHtml.IndexOf("<tr valign=\"top\"><td align=\"right\"><font size=\"2\">", next + 1);

                        flag = next >= 0;
                    }

                    //查找blank
                    int posBlank = pageHtml.IndexOf("独件冲裁：");
                    int posX1 = pageHtml.IndexOf(" x", posBlank);
                    int posX2 = pageHtml.IndexOf(" x", posX1 + 3);
                    int posBlankEnd = pageHtml.IndexOf("<!--", posX2);
                    string blank = pageHtml.Substring(posX2 + 4, posBlankEnd - posX2 - 4);
                    blank = blank.Replace(Environment.NewLine, "").Trim();

                    sb.Append(blank);

                    if (!Directory.Exists(path))//若文件夹不存在则新建文件夹   
                    {
                        Directory.CreateDirectory(path); //新建文件夹   
                    }

                    //日期
                    int pos0 = pageHtml.IndexOf("<br />", pageHtml.IndexOf("<br />") + 6);
                    string date = pageHtml.Substring(pos0 + 17, 10);

                    if (list.Count > 0)
                    {
                        QRCodeHandler qr = new QRCodeHandler();
                        string qrString = sb.ToString();                         //二维码字符串
                        string filePath = path + name + ".jpg";                                        //二维码文件名
                        qr.CreateQRCode(qrString, "Byte", 2, 0, "H", filePath, false, string.Empty);   //生成

                        //插入二维码
                        int posPic = pageHtml.IndexOf("</table>", pageHtml.IndexOf("</table>") + 8) + 8;
                        pageHtml = pageHtml.Insert(posPic, "</div><div style='float:right'><img src=\"" + name + ".jpg\"/><br><br>" + date + "</div></div>");
                    }
                    else
                    {
                        //插入日期
                        int posPic = pageHtml.IndexOf("</table>", pageHtml.IndexOf("</table>") + 8) + 8;
                        pageHtml = pageHtml.Insert(posPic, "</div><div style='float:right'>" + date + "</div></div>");
                    }

                    //改变文件内容
                    int pos1 = pageHtml.IndexOf("<table");
                    int pos14 = pos1;
                    int pos2 = pageHtml.IndexOf("</table>");
                    pageHtml = DelMidStr(pageHtml, pos1, pos2 + 8);

                    pos1 = pageHtml.IndexOf("<table");
                    pageHtml = pageHtml.Insert(pos1, @"<div style='width:750px'><div style='float:left'>");
                    pos2 = pageHtml.IndexOf("<!--HTML-Block: Fertigungsanweisungen-->");
                    pageHtml = pageHtml.Insert(pos2, @"<div style='margin-top:250px;width:750px'>");
                    int pos3 = pageHtml.IndexOf("<!--ENDE Tafellayout-->");
                    pageHtml = pageHtml.Insert(pos3, @"</div>");

                    //删除
                    int pos4 = pageHtml.IndexOf("公司：");
                    int pos5 = pageHtml.IndexOf("<tr valign", pos4 - 50);
                    int pos6 = pageHtml.IndexOf("<tr valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    pos4 = pageHtml.IndexOf("库存名称：");
                    pos5 = pageHtml.IndexOf("<tr valign", pos4 - 50);
                    pos6 = pageHtml.IndexOf("<tr valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    pos4 = pageHtml.IndexOf("储存体需求量：");
                    pos5 = pageHtml.IndexOf("<tr valign", pos4 - 50);
                    pos6 = pageHtml.IndexOf("<tr valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    pos1 = pageHtml.IndexOf("<!--Pratzendaten-->");
                    pos2 = pageHtml.IndexOf("<!--", pos1 + 3);
                    string s1 = pageHtml.Substring(pos1, pos2 - pos1);

                    pos1 = pageHtml.IndexOf("<!--Einlegeposition-->");
                    pos2 = pageHtml.IndexOf("<!--", pos1 + 3);
                    s1 += pageHtml.Substring(pos1, pos2 - pos1);

                    pos1 = pageHtml.IndexOf("<!--Bemerkungen (z.B. Zusatzauflagetische)-->");
                    pageHtml = pageHtml.Insert(pos1, s1);

                    pos1 = pageHtml.IndexOf("<!--START Fertigungsanweisungen-->");
                    pos2 = pageHtml.IndexOf("<!--ENDE Fertigungsanweisungen-->");
                    pageHtml = DelMidStr(pageHtml, pos1, pos2);

                    //替换
                    pageHtml = pageHtml.Replace("ISO-2022-CN", "utf-8");

                    //表格宽度
                    pageHtml = pageHtml.Replace("<table width=\"600\"", "<table width=\"750\"");

                    int pos10 = pageHtml.IndexOf("<table width=\"750\"");
                    pageHtml = DelMidStr(pageHtml, pos10, pos10 + 16);
                    pageHtml = pageHtml.Insert(pos10, "<table width=\"600\"");

                    //计算秒
                    int pos11 = pageHtml.IndexOf("<!--Maschinenzeit in [h:min:s] -->");
                    int pos12 = pageHtml.IndexOf("[h:min:s]&nbsp;", pos11);
                    string time = pageHtml.Substring(pos11 + 42, pos12 - pos11 - 42);

                    string[] arr = time.Split(new string[] { " : " }, StringSplitOptions.None);
                    int hour = Convert.ToInt32(arr[0]);
                    int min = Convert.ToInt32(arr[1]);
                    int sec = Convert.ToInt32(arr[2]);

                    int secornd = hour * 60 * 60 + min * 60 + sec;

                    int pos13 = pageHtml.IndexOf("</b>", pos12 + 1);
                    pageHtml = pageHtml.Insert(pos13, secornd.ToString() + "秒");

                    //加签名
                    //int pos14 = pageHtml.IndexOf("<!--HTML-Block: Stanz-Werkzeugliste-->");
                    pageHtml = pageHtml.Insert(pos14, "完成日期：______________   系数：_______   操作员：___________   完成张数：_______   总数：__________<BR><BR><BR>完成日期：______________   系数：_______   操作员：___________   完成张数：_______   总数：__________<BR><BR>");

                    string extension = System.IO.Path.GetExtension(openFileDialog.FileName);
                    string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);

                    using (StreamWriter sw = new StreamWriter(path + fileNameWithoutExtension + "_barcode" + extension))//将获取的内容写入文本
                    {
                        sw.Write(pageHtml);
                    }

                    //打开文件
                    System.Diagnostics.Process.Start(path + fileNameWithoutExtension + "_barcode" + extension);
                }

                catch (WebException webEx)
                {
                    Console.WriteLine(webEx.Message.ToString());
                }
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "网页|*.html";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fileName = openFileDialog.SafeFileName;

                    path = openFileDialog.FileName.Substring(0, openFileDialog.FileName.Length - fileName.Length);

                    WebClient MyWebClient = new WebClient();

                    MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据

                    Byte[] pageData = MyWebClient.DownloadData(openFileDialog.FileName); //从指定网站下载数据

                    string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312，则使用这句            

                    if (!Directory.Exists(path))//若文件夹不存在则新建文件夹   
                    {
                        Directory.CreateDirectory(path); //新建文件夹   
                    }

                    //替换
                    pageHtml = pageHtml.Replace("ISO-2022-CN", "utf-8");

                    //日期
                    int pos0 = pageHtml.IndexOf("<br />", pageHtml.IndexOf("<br />") + 6);
                    string date = pageHtml.Substring(pos0 + 17, 10);
                    int posPic = pageHtml.IndexOf("</table>", pageHtml.IndexOf("</table>") + 8) + 8;
                    pageHtml = pageHtml.Insert(posPic, "</div><div style='float:right'>" + date + "</div></div>");

                    //改变文件内容
                    int pos1 = pageHtml.IndexOf("<table");
                    int pos14 = pos1;
                    int pos2 = pageHtml.IndexOf("</table>");
                    pageHtml = DelMidStr(pageHtml, pos1, pos2 + 8);

                    pos1 = pageHtml.IndexOf("<table");
                    pageHtml = pageHtml.Insert(pos1, @"<div style='width:750px'><div style='float:left'>");
                    pos2 = pageHtml.IndexOf("<!--HTML-Block: Fertigungsanweisungen fuer LaserOnly-->");
                    pageHtml = pageHtml.Insert(pos2, @"<div style='margin-top:250px;width:750px'>");
                    int pos3 = pageHtml.IndexOf("<!--ENDE Tafellayout-->");
                    pageHtml = pageHtml.Insert(pos3, @"</div>");

                    //删除
                    int pos4 = pageHtml.IndexOf("公司：");
                    int pos5 = pageHtml.IndexOf("<tr valign", pos4 - 50);
                    int pos6 = pageHtml.IndexOf("<tr valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    pos4 = pageHtml.IndexOf("库存名称：");
                    pos5 = pageHtml.IndexOf("<tr valign", pos4 - 50);
                    pos6 = pageHtml.IndexOf("<tr valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    pos4 = pageHtml.IndexOf("储存体需求量：");
                    pos5 = pageHtml.IndexOf("<tr valign", pos4 - 50);
                    pos6 = pageHtml.IndexOf("<tr valign", pos5 + 1);
                    pageHtml = DelMidStr(pageHtml, pos5, pos6 - 1);

                    //表格宽度
                    pageHtml = pageHtml.Replace("<table width=\"600\"", "<table width=\"750\"");

                    int pos10 = pageHtml.IndexOf("<table width=\"750\"");
                    pageHtml = DelMidStr(pageHtml, pos10, pos10 + 16);
                    pageHtml = pageHtml.Insert(pos10, "<table width=\"600\"");

                    //计算秒
                    int pos11 = pageHtml.IndexOf("<nobr>");
                    int pos12 = pageHtml.IndexOf("[h:min:s]", pos11);
                    string time = pageHtml.Substring(pos11 + 6, pos12 - pos11 - 6);

                    string[] arr = time.Split(new string[] { " : " }, StringSplitOptions.None);
                    int hour = Convert.ToInt32(arr[0]);
                    int min = Convert.ToInt32(arr[1]);
                    int sec = Convert.ToInt32(arr[2]);

                    int secornd = hour * 60 * 60 + min * 60 + sec;

                    int pos13 = pageHtml.IndexOf("</b>", pos12 + 1);
                    pageHtml = pageHtml.Insert(pos13, secornd.ToString() + "秒");

                    //加签名
                    //int pos14 = pageHtml.IndexOf("<!--HTML-Block: Stanz-Werkzeugliste-->");
                    pageHtml = pageHtml.Insert(pos14, "完成日期：______________   系数：_______   操作员：___________   完成张数：_______   总数：__________<BR><BR><BR>完成日期：______________   系数：_______   操作员：___________   完成张数：_______   总数：__________<BR><BR>");

                    string extension = System.IO.Path.GetExtension(openFileDialog.FileName);
                    string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);

                    using (StreamWriter sw = new StreamWriter(path + fileNameWithoutExtension + "_barcode" + extension))//将获取的内容写入文本
                    {
                        sw.Write(pageHtml);
                    }

                    //打开文件
                    System.Diagnostics.Process.Start(path + fileNameWithoutExtension + "_barcode" + extension);
                }

                catch (WebException webEx)
                {
                    Console.WriteLine(webEx.Message.ToString());
                }
            }
        }
    }
}
